<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by JetBrains PhpStorm.
 * User: User
 * Date: 2/19/13
 * Time: 2:29 PM
 * To change this template use File | Settings | File Templates.
 */
class Article extends Admin {
    private $_table = "article";
    public function __construct()
    {
        parent::__construct();
        $this->load->model("article_model");
    }

    public function index(){
        $page = $this->uri->segment(4);
        $param=array();
        $page_size = 20;
        $param['keyword']=$this->input->get_post('keyword');
        $total = $this->article_model->count_all($param);
        $records = $this->article_model->find_all($page, $page_size,$param);
        $page_url = base_url('/cp/article/index');
        $pagination = $this->get_search_paging($page_url, $total, $page, $page_size );
        $data = array(
            "total" => $total,
            "records" => $records,
            "pagination" => $pagination,
            'param'=>$param
        );
        $this->layout->view($data);
    }

    public function add(){
        $data=array();
        $this->form_validation->set_rules('title', 'title', 'required');
        $this->form_validation->set_rules('alias', 'alias', 'required');
        $this->form_validation->set_rules('content', 'content', 'required');
        if ($this->form_validation->run() == TRUE){
            $obj =array();
            $obj['title']=$this->input->post('title');
            $obj['alias']=trim($this->input->post('alias'));
            $obj['content']=$this->input->post('content');
            $obj['status']=1;
            $obj['createdTime']=time();
            $this->db->insert($this->_table,$obj);
            if($this->db->insert_id()){
                redirect('cp/article');
            }
        }
        $this->layout->view($data);
    }
    public function edit($id=null){
        $data=array();
        $this->form_validation->set_rules('title', 'title', 'required');
        $this->form_validation->set_rules('alias', 'alias', 'required');
        $this->form_validation->set_rules('content', 'content', 'required');
        if ($this->form_validation->run() == TRUE){
            $obj =array();
            $obj['title']=$this->input->post('title');
            $obj['alias']=trim($this->input->post('alias'));
            $obj['content']=$this->input->post('content');
            $obj['status']=1;
            $obj['createdTime']=time();
            $id=$this->input->post('id');
            $this->db->where("id",$id);
            $this->db->update($this->_table,$obj);
            redirect('cp/article');
        }
        $data['item']=$this->article_model->getSingle($id);
        $this->layout->view($data);
    }
}
